<template>
  <Layout>
    <div class="detail" ref="mainContainer" v-loading="isLoading">
      <Content :info="data" v-if="isLoading" />
      <Comment v-if="isLoading" />
    </div>
    <template #footer>
      <div class="right-container" v-loading="isLoading">
        <ContentList :list="data.toc" v-if="data" />
      </div>
    </template>
  </Layout>
</template>

<script>
import Layout from "@/components/Layout";
import Content from "./components/Content";
import ContentList from "./components/ContentList";
import Comment from "./components/Comment";
import { getBlogDetail } from "@/api/blog.js";
import featchData from "@/mixins/fetchData.js";
import handleScroll from "@/mixins/handleScroll.js";
import setTitle from "@/utils/setTitle";

export default {
  components: {
    Layout,
    Content,
    ContentList,
    Comment,
  },
  // 混入数据
  mixins: [featchData(null), handleScroll("mainContainer")],
  methods: {
    // 获取数据方法
    async fetchData() {
      // 请求数据
      let data = await getBlogDetail(this.$route.params.blogId);

      // 没有请求到数据的时候，跳转到 404 页面
      if (!data) {
        this.$router.push({
          name: "404",
        });
        return;
      }

      // 设置文章详情页的 title
      setTitle.setRouteTitle(data.title);
      return data;
    },
  },
  updated() {
    // 初始加载页面，跳转到 url 中指定的锚点；

    // 1- 先清空地址栏中的 hash
    const hash = location.hash;
    location.hash = "";

    // 2- 在微队列中运行一个函数，将保存的hash值，重新设置到地址栏；
    setTimeout(() => {
      location.hash = hash;
    }, 50);
  },
};
</script>

<style lang="less" scoped>
.detail {
  width: 100%;
  height: 100%;
  padding: 15px;
  box-sizing: border-box;
  overflow-x: hidden;
  overflow-y: scroll;
  scroll-behavior: smooth;
  position: relative;
}

.right-container {
  width: 250px;
  height: 100%;
  overflow-y: scroll;
  box-sizing: border-box;
  position: relative;
  padding: 20px;
}
</style>